package bc.swim2.ejb.session;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import bc.swim2.ejb.entity.AbilityRequest;

/**
 * Session Bean implementation class AbilityRequestManagerBean
 */
@Stateless
public class AbilityRequestManagerBean implements
		AbilityRequestManagerBeanRemote {

	@PersistenceContext(unitName = "swimv2")
	private EntityManager em;

	/**
	 * Default constructor.
	 */
	public AbilityRequestManagerBean() {
	}

	@SuppressWarnings("unchecked")
	@Override
	public void removeAbilityRequest(int abilityRequestId) {

		Query q = em.createNamedQuery("Ability.getAbilityRequestById");
		q.setParameter("id", abilityRequestId);
		List<AbilityRequest> arl;
		arl = q.getResultList();
		em.remove(arl.get(0));

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AbilityRequest> getAbilityRequestList() {
		List<AbilityRequest> abilityRequests = null;

		try {
			Query q = em.createNamedQuery("Ability.getAbilityRequestList");
			abilityRequests = (List<AbilityRequest>) q.getResultList();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		return abilityRequests;
	}

	@Override
	public void insertAbilityRequest(String arName, String arDescription) {
		try {
			AbilityRequest newAbilityRequest = new AbilityRequest();
			newAbilityRequest.setName(arName);
			newAbilityRequest.setDescription(arDescription);
			em.persist(newAbilityRequest);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}
